<template>
  <div class="et-login">
    <et-header title="登录注册" @backPage="backPage"></et-header>

    <div class="et-login-content">
      <div class="content-menu">
        <div class="menu-btn menu-title01 menu-active">登录</div>
        <div class="menu-btn menu-title02" @click="toRegPage">注册</div>
      </div>
    </div>

    <!--登录注册From-->
    <div class="et-login-box">
      <div class="wrap-box">
        <div class="icon1"><img src="../images/login/phone.png" class="phone1" onclick="return false"/></div>
        <div class="input-box"><input type="text" class="phone2" placeholder="请输入手机号码" maxlength="11" v-model="phone"
                                      v-on:input="checkIcon"/>
        </div>
        <div class="icon2" v-if="showIcon"><img src="../images/login/yes.png" class="phone3" onclick="return false"/>
        </div>
        <div class="icon2" v-if="hideIcon"><img src="../images/login/no.png" class="phone3" onclick="return false"/>
        </div>
      </div>
      <div class="wrap-box distance">
        <div class="icon3"><img src="../images/login/admin.png" class="pwd1" onclick="return false"/></div>
        <div class="pwd-box"><input :type="pwdType" class="pwd2" placeholder="请输入6-16位密码" maxlength="16"
                                    v-model="password" @blur="inputBlur"/></div>
        <div class="icon4" @click="changeType"><img :src=pwdIcon class="pwd3" onclick="return false"/></div>
      </div>
      <div class="opt-box">
        <div class="select-box"><input type="checkbox" class="checkbox" :class="{'select-icon': choice}"
                                       @click="checkItem"/></div>
        <div class="remember">记住密码</div>
        <div class="forget" @click="forget">忘记密码?</div>
      </div>

      <div class="btn-box">
        <button type="button" class="login" @click="login">登录</button>
      </div>
    </div>
  </div>

</template>

<script>
  import md5 from 'js-md5';
  import EtHeader from "../components/EtHeader";
  import {getStore, getUrlKey, setSessionStore, setStore} from "../../../config/util";
  import {baseUrl} from "../../../config/env";

  export default {
    name: "etLogin",
    components: {
      EtHeader
    },
    data() {
      return {
        choice: true,
        showIcon: false,
        hideIcon: false,
        phone: "",
        password: "",
        pwdType: "password",
        pwdIcon: require("../images/login/eyes_close.png"),
        workId: getUrlKey('id'),
        pageType: getUrlKey('pageType'),
        identity: ''
      }
    },
    created() {
      let phone = getStore("et_local_phone");
      let pwd = getStore("et_local_pwd");
      let base64 = require('js-base64').Base64;
      if (pwd) {
        this.phone = phone;
        this.password = base64.decode(pwd);
      } else {
        this.phone = phone;
        this.password = "";
      }
    },
    methods: {
      backPage() {
        if (this.pageType == 2) {
          this.$router.push({
              path: '/jobQuick',
              query: {
                'workAddress': this.$route.query.workAddress,
                'workPost': this.$route.query.workPost,
                'workWage': this.$route.query.workWage,
                'age': this.$route.query.age,
                'sex': this.$route.query.sex
              }
            }
          );
        } else {
          this.$router.push({path: "/jobDetail", query: {'id': this.workId}});
        }
      },
      checkItem() {
        this.choice = !this.choice;
      },
      checkIcon() {//手机号icon
        let phone = this.phone;
        let reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
        if (!reg.test(phone.trim())) {
          this.showIcon = false;
          this.hideIcon = true;
        } else {
          this.showIcon = true;
          this.hideIcon = false;
        }
      },
      changeType() {//密码icon
        this.pwdType = this.pwdType === 'password' ? 'text' : 'password';
        this.pwdIcon = this.pwdIcon == require("../images/login/eyes_open.png") ? require("../images/login/eyes_close.png") : require("../images/login/eyes_open.png");
      },
      checkPwd(pwd) {//校验密码
        if (pwd == "" || pwd == undefined) {
          this.$vux.toast.text("密码不能为空", "middle");
          return;
        } else if (pwd.indexOf(" ") >= 0) {
          this.$vux.toast.text("密码中不能有空格字符", "middle");
          return;
        } else if ((/[\u4e00-\u9fa5]+/).test(pwd)) {
          this.$vux.toast.text("密码不能包含中文", "middle");
          return;
        }
        return true;
      },
      checkPhone(phone) {//校验手机号码
        let reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
        if (phone == "" || phone == undefined) {
          this.$vux.toast.text("手机号码不能为空", "middle");
          return;
        }
        if (!reg.test(phone)) {
          this.$vux.toast.text("手机号码格式错误", "middle");
          return;
        }
        return true;
      },
      checkCode() {//校验验证码
        let reg = /^[0-9]*$/;
        let code = this.codeNum;
        if (code == "" || code == undefined) {
          this.$vux.toast.text("验证码不能为空", "middle");
          return;
        } else if (!reg.test(code)) {
          this.$vux.toast.text("验证码格式不正确", "middle");
          return;
        }
        return true;
      },
      rememberPwd() {//记住密码
        let phone = this.phone;
        let pwd = this.password;
        if (this.choice) {
          let base64 = require('js-base64').Base64;
          setStore("et_local_phone", phone);
          setStore("et_local_pwd", base64.encode(pwd));
        } else {
          localStorage.removeItem("et_local_pwd");
        }
      },
      login() {//登录
        let phone = this.phone;
        let pwd = this.password;
        if (this.checkPhone(phone) && this.checkPwd(pwd)) {
          this.$vux.loading.show({
            text: '加载中',
          });

          let data = {"phone": phone, "password": md5(pwd), "type": 0, "upIdentity": this.identity};
          this.axios.post(baseUrl + "/login/login", data).then((res) => {
            let resultObj = res.data;
            this.$vux.loading.hide();
            if (resultObj.data == "null") {
              this.$vux.toast.text("用户名或密码错误", "middle");
              return;
            } else if (resultObj.data == "error") {
              this.$vux.toast.text("用户名或密码错误", "middle");
              return;
            } else {
              this.rememberPwd();
              let base64 = require('js-base64').Base64;
              let userArray = resultObj.data.split("#");
              setSessionStore("et_user_sign", base64.encode(userArray[0]));
              if (this.pageType == 2) {
                this.$router.push({
                  path: "/jobQuick",
                  query: {
                    'workAddress': this.$route.query.workAddress,
                    'workPost': this.$route.query.workPost,
                    'workWage': this.$route.query.workWage,
                    'age': this.$route.query.age,
                    'sex': this.$route.query.sex
                  }
                });
              } else {
                this.$router.push({path: "/jobDetail", query: {'id': this.workId, "markType": 1}});
              }
            }
          }).catch(() => {
            this.$vux.loading.hide();
            this.$vux.toast.text("网络异常", "middle");
          });
        }
      },
      forget() {
        window.location.href = "https://url.cn/5AUAFDU";
      },
      inputBlur() {
        window.scrollTo(0, 0);
      },
      toRegPage() {
        this.$router.push({
          path: '/etRegister',
          query: {
            'id': this.workId,
            'pageType': this.pageType,
            'workAddress': this.$route.query.workAddress,
            'workPost': this.$route.query.workPost,
            'workWage': this.$route.query.workWage,
            'age': this.$route.query.age,
            'sex': this.$route.query.sex
          }
        });
      }
    }
  }
</script>

<style lang="stylus" scoped>
  @import "../../../assets/stylus/mixin.styl"

  .et-login
    wh(100%, 100%)
    background: #f5f5f5
    .et-login-content
      display: flex
      position: relative
      wh(100%, 4.587rem)
      bg-image("../images/login/bg.png", 100%, 100%)
      .content-menu
        display: flex
        position: absolute
        left: 0
        right: 0
        bottom: -0.373rem
        margin: auto
        wh(4.8rem, 0.853rem)
        border-radius: 0.427rem
        box-shadow: 0rem 0rem 0.267rem 0rem rgba(0, 0, 0, 0.2)
        background: #fff
        .menu-btn
          flex: 1
          font-size: 0.427rem
          fj(center, center)
          color: rgba(60, 175, 255, 1)
        .menu-title01
          border-top-left-radius: 0.427rem
          border-bottom-left-radius: 0.427rem
        .menu-title02
          border-top-right-radius: 0.427rem
          border-bottom-right-radius: 0.427rem
        .menu-active
          color: #fff
          background: linear-gradient(-90deg, rgba(165, 130, 255, 1), rgba(60, 175, 255, 1))
    .et-login-box
      width: 100%
      margin-top: 0.8rem
      .wrap-box
        display: flex
        align-items: center
        wh(82.67%, 1.2rem)
        margin: 0 auto
        border-bottom: 0.013rem solid #E0E0E0
        margin-bottom: 0.48rem
        .icon1
          width: 0.787rem
          .phone1
            wh(0.533rem, 0.533rem)
        .icon3
          width: 0.787rem
          .pwd1
            wh(0.533rem, 0.533rem)
        .input-box
          width: auto
          .phone2
            wh(100%, 100%)
            size-color(0.427rem, #333)
            background: #f5f5f5
        .pwd-box
          width: 6.947rem
          .pwd2
            wh(100%, 100%)
            size-color(0.427rem, #333)
            background: #f5f5f5
        .icon2
          wh(0.533rem, 0.533rem)
          margin-left: auto
          .phone3
            wh(100%, 100%)
        .icon4
          wh(0.533rem, 0.533rem)
          margin-left: auto
          .pwd3
            wh(100%, 100%)
      .distance
        margin-bottom: 0.4rem
      .opt-box
        display: flex
        align-items: center
        margin: 0 auto
        margin-bottom: 0.96rem
        wh(82.67%, 0.32rem)
        font-size: 0.32rem
        .select-box
          wh(0.32rem, 0.32rem)
          margin-right: 0.24rem
          margin-left: 0.1rem
          .checkbox
            width: 100%
            height: 100%
            outline: none
            border: 1px solid #333
            border-radius: 0.04rem
            -webkit-appearance: none
          .select-icon
            background: url("../images/login/hock.png") no-repeat center rgba(60, 175, 255, 1)
            border-radius: 0.04rem
            background-size: 0.227rem 0.173rem
            border: none
        .remember
          color: #666666
        .forget
          color: #3CAFFF
          margin-left: auto

      .btn-box
        wh(100%, 1.28rem)
        text-align: center
        .login
          wh(82.67%, 100%)
          line-height: 1.333rem
          background: rgba(60, 175, 255, 1)
          border-radius(0.16rem)
          size-color(0.453rem, rgba(255, 255, 255, 1))

</style>
